package io.kimmking.rpcfx.config.center.zookeeper;

import com.alibaba.fastjson.JSON;
import io.kimmking.rpcfx.config.center.api.ConfigInfo;
import io.kimmking.rpcfx.metadata.center.api.MetadataRegister;
import io.kimmking.rpcfx.metadata.center.zookeeper.ZookeeperMetadataRegister;
import io.kimmking.rpcfx.register.center.api.ProviderInfo;
import io.kimmking.rpcfx.register.center.zookeeper.ZookeeperRegister;
import org.apache.curator.framework.CuratorFramework;

import java.security.Provider;

public class InitConfig {

    public static void main(String[] args) throws Exception {
        ZookeeperConfigCenter cc = new ZookeeperConfigCenter("127.0.0.1:2181", "rpcfx_config");
        ProviderInfo info = new ProviderInfo();
        info.setTag("config");
        ConfigInfo orderConfigInfo = ConfigInfo.builder().serverName("io.kimmking.rpcfx.demo.api.OrderService").build();
        cc.setStringConfig(orderConfigInfo, JSON.toJSONString(info));

        ConfigInfo userConfigInfo = ConfigInfo.builder().serverName("io.kimmking.rpcfx.demo.api.UserService").build();
        cc.setStringConfig(userConfigInfo, JSON.toJSONString(info));
        cc.close();
        ZookeeperRegister zr = new ZookeeperRegister("127.0.0.1:2181", "rpcfx");
        CuratorFramework client = zr.getClient();
        try {
            client.delete().deletingChildrenIfNeeded().forPath("/io.kimmking.rpcfx.demo.api.OrderService");
        } catch (Exception ex) {
            //
        }
        try {
            client.delete().deletingChildrenIfNeeded().forPath("/io.kimmking.rpcfx.demo.api.UserService");
        } catch (Exception ex) {
            //
        }

        ZookeeperMetadataRegister zookeeperMetadataRegister = new ZookeeperMetadataRegister("127.0.0.1:2181", "rpcfx_metadata");
        client = zookeeperMetadataRegister.getClient();
        try {
            client.delete().deletingChildrenIfNeeded().forPath("/io.kimmking.rpcfx.demo.api.OrderService");
        } catch (Exception ex) {
            //
        }
        try {
            client.delete().deletingChildrenIfNeeded().forPath("/io.kimmking.rpcfx.demo.api.UserService");
        } catch (Exception ex) {
            //
        }
        System.exit(0);
    }
}
